package com.example.plat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.plat.enity.User;
import com.example.plat.exception.UserException;
import com.example.plat.mapper.UserMapper;
import com.example.plat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper mapper;


    @Override
    public User findUser(String userName) {
        return mapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName,userName));
    }

    @Transactional
    @Override
    public User addUser(User user) throws UserException{
        try {
            mapper.insert(user);
        }catch (Exception e){
            throw new UserException("添加失败，用户名重复!");
        }
        return user;
    }

    @Override
    public void deleteUser(Long userId) throws UserException{
        try {
            mapper.deleteById(userId);
        }catch (Exception e){
            throw new UserException("删除失败!");
        }
    }

    @Override
    public User changeUser(User user) throws UserException{
        try {
            mapper.updateById(user);
        }catch (Exception e){
            throw new UserException("修改用户失败!");
        }
        return user;
    }

    //---------------
}
